package com.lyt.rfid_demo;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileOpertor {
    public static String TAG = "fm1702";

    public static List<String> readFileByLines(String fileName) {
        File file = new File(fileName);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (IOException e2) {
            e2.printStackTrace();
        }

        List<String> list = new ArrayList<String>();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;

            while ((tempString = reader.readLine()) != null) {
                //Log.i(TAG, "read^^^^" );
                list.add(tempString);
            }
            reader.close();
        } catch (IOException e) {
            Log.i(TAG, "can't find file " + fileName);
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return list;
    }

    public static void writeFile(String fileName, String message) {
        try {
            Log.i(TAG, "writefile^^^^");
//			File file = new File(fileName);
//			try {
//				if (!file.exists()) {
//					file.createNewFile();
//				}
//			} catch (IOException e2) {
//				e2.printStackTrace();
//			}
            FileOutputStream fout = new FileOutputStream(fileName);
            byte[] bytes = message.getBytes();
            fout.write(bytes);
            fout.flush();
            fout.close();
        } catch (Exception e) {
            Log.i(TAG, "can't find file " + fileName);
            e.printStackTrace();
        }

    }

    public static void backupFile(String fileName, String fileName_bak) {
        File file = new File(fileName);
        File file_bak = new File(fileName_bak);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            if (!file_bak.exists()) {
                file_bak.createNewFile();
            }
        } catch (IOException e2) {
            e2.printStackTrace();
        }

        /*
         *add by jyq
         */
        if (file.length() >= file_bak.length()) {
            Log.d(TAG, "back up log file");
            if (!copyFile(fileName, fileName_bak)) {
                Log.e(TAG, "log file bak fail ");
                Log.e(TAG, "log file bak fail ");
                Log.e(TAG, "log file bak fail ");
            }
        } else {
            Log.e(TAG, "log lost!!! ");
        }
    }

    public static boolean copyFile(String from, String to) {

        File fromFile, toFile;

        fromFile = new File(from);

        toFile = new File(to);

        FileInputStream fis = null;

        FileOutputStream fos = null;

        try {

            toFile.createNewFile();

            fis = new FileInputStream(fromFile);

            fos = new FileOutputStream(toFile);

            int bytesRead;

            byte[] buf = new byte[256 * 1024];//256K buffer

            while ((bytesRead = fis.read(buf)) != -1) {

                fos.write(buf, 0, bytesRead);

            }

            fos.flush();

            fos.close();

            fis.close();

        } catch (IOException e) {

            return false;

        }

        return true;

    }


}
